package net.tv90.bfmccore.task;

import net.tv90.bfmccore.SlainListener;
import net.tv90.bfmccore.api.info.PlayerStatistic;
import net.tv90.bfmccore.battlepass.Battlepass;
import net.tv90.bfmccore.helper.MCHHelper;
import org.bitterorange.flansmodslain.FlansModSlain;
import org.bitterorange.flansmodslain.actions.Money;
import org.bitterorange.flansmodslain.models.SoldierDeathEvent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class BFMCMainTask implements Runnable {


    long time = 0L;
    long mchClearTime = 0L;

    /**
     * 在线奖励时间间隔
     */
    public static final long ONLINE_TIME_DELAY = 30 * 60L;

    /**
     * 每分钟保存一次通行证数据
     */
    private static final long BATTLEPASS_SAVE_TIME_DELAY = 60L;

    public static String[] announcements = {
            "§e[自动公告]可使用 §b/vote kick <玩家名> §e来进行踢除投票.",
            "§e[自动公告]提示: 开镜并蹲下可获得最高的精确度与最低的后坐力.",
            "§e[自动公告]可使用 §b/suicide §e命令来自杀.",
            "§e[自动公告]使用 §b选项-控制-BFMC主菜单 §e对应的按键来打开复活菜单.",
            "§e[自动公告]使用 §b/bps info §e查看你的战斗通行证信息.",
            "§e[自动公告]使用 §b/bp open §e打开战斗包."
    };

    public static int currentAnnouncementIndex = 0;
    public static final int ANNOUNCEMENT_TIME_INTERVAL = 10;

    @Override
    public void run() {

        time++;
        mchClearTime++;
        if(time > Long.MAX_VALUE - 1L) time = 0L;
        if(mchClearTime > Long.MAX_VALUE - 1L) mchClearTime = 0L;

        if(mchClearTime % 100L == 0L){
            Bukkit.broadcastMessage("§e[管理员]即将清理废弃载具(20s)");
        }
        if(mchClearTime % 105L == 0L){
            Bukkit.broadcastMessage("§e[管理员]即将清理废弃载具(15s)");
        }
        if(mchClearTime % 110L == 0L){
            Bukkit.broadcastMessage("§e[管理员]即将清理废弃载具(10s)");
        }
        if(mchClearTime % 115L == 0L){
            Bukkit.broadcastMessage("§e[管理员]即将清理废弃载具(5s)");
        }
        if(mchClearTime % 120L == 0L){
            MCHHelper.clearMCHEntities();
            mchClearTime = 0L;
        }

        // 自动公告
        if (time % ANNOUNCEMENT_TIME_INTERVAL == 0L) {
            if (announcements.length > 0) {
                Bukkit.broadcastMessage(announcements[currentAnnouncementIndex]);
                currentAnnouncementIndex = (currentAnnouncementIndex + 1) % announcements.length;
            }
        }


        if(time % ONLINE_TIME_DELAY == 0L){
            for (Player p : Bukkit.getOnlinePlayers()) {
                p.sendMessage("§e[管理员]在线奖励(200游戏币，50军衔)已发放，下次奖励将在30分钟后发放");
                Money.give(p, 200);
                FlansModSlain.addPoints(p, 50);
            };
        }

        if(time % BATTLEPASS_SAVE_TIME_DELAY == 0L){
            Battlepass.storeBattlepassData();
            Battlepass.storeDailyLimitData();
        }

        if(time % 30L == 0L) {
            FlansModSlain.saveData();
        }

        if(time % 3600L == 0L) {
            for (Player player : Bukkit.getOnlinePlayers()) {
                Battlepass.addBattlepassPoint(player, Battlepass.POINT_PER_HALF_HOUR);
                player.sendMessage("§e[管理员]在线奖励" + Battlepass.POINT_PER_HALF_HOUR + "通行证点数已发放，下次奖励将在30分钟后发放");
            }
        }

        if(time % 180L == 0L) {
            SlainListener.saveAIKillLog();
        }

        if(time % 60L == 0L) {
            for (Player player : Bukkit.getOnlinePlayers()) {
                PlayerStatistic.savePlayerStatistic(player.getName());
            }
            System.out.println("已保存当前在线的玩家数据");
            new Thread(() -> {
                PlayerStatistic.savePlayerStatisticsToFile();
                System.out.println("已序列化所有玩家的统计数据");
            }).start();
        }

        //检测系统时间，如果是晚上2点，重启服务器
//        Calendar calendar = Calendar.getInstance();
//        int hour = calendar.get(Calendar.HOUR_OF_DAY);
//        int minute = calendar.get(Calendar.MINUTE);
//        if(hour == 2 && minute == 0){
//            new Thread(() -> {
//                Bukkit.broadcastMessage("§e[管理员]服务器将在1分钟后自动重启");
//                try {
//                    Thread.sleep(1000L * 60);
//                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "stop");
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }).start();
//        }

    }
}
